<template>
    <div>
        <el-row>
            <el-col :span="8">
                <el-input type="textarea" v-model="source" style="height: 500px"></el-input>
            </el-col>
            <el-col :span="2">
                <el-button type="primary" @click="doTransfer"> 转换>> </el-button>
            </el-col>
            <el-col :span="8">
                <el-table border stripe :data="listData">
                    <el-table-column type="index" label="序号" width="60"></el-table-column>
                    <el-table-column label="key">
                        <template slot-scope="scope">
                            {{scope.row.key}}
                        </template>
                    </el-table-column>
                    <el-table-column label="doc_count">
                        <template slot-scope="scope">
                            {{scope.row.doc_count}}
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>

    </div>
</template>

<script>
    export default {
        name: "EsTransfer",
        data() {
            return {
                type: 'bulks', // bulks
                source: null,
                listData: []
            }
        },
        created() {
        },
        mounted() {
        },
        methods: {
            doTransfer() {
                switch (this.type) {
                    case "bulks":
                        this.transferBulks()
                        break
                    default:
                        break
                }
                console.log('转换')
            },
            transferBulks() {
                if (!this.source) {
                    return
                }
                let tmpList
                try {
                    tmpList = JSON.parse(this.source)
                } catch (e) {
                    console.log(e)
                    this.$message.success('非json格式数据，无法转换')
                    return;
                }

                if (!(tmpList instanceof Array)) {
                    this.$message.success('数据需要数组格式，请检查')
                    return
                }
                this.listData = tmpList
                console.log('this.listData: ', this.listData)
            }
        }
    }
</script>

<style scoped>

</style>